package org.easyframework.activemq.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @ClassName: ActiveMQConfig
 * @Description: ActiveMQ配置类
 * @author: daiw
 * @date: 2019年10月15日 下午4:38:01
 */
@Configuration
@Component
public class ActiveMQConfig {
	@Value("${ccbao.core.active.mq.userName}")
	private String usrName;

	@Value("${ccbao.core.active.mq.password}")
	private String password;

	@Value("${ccbao.core.active.mq.brokerurl}")
	private String brokerUrl;

	@Bean(name = "jmsTemplate")
	public JmsTemplate getJmsTemplate() {
		return new JmsTemplate(connectionFactory());
	}

	@Bean
	public ActiveMQConnectionFactory connectionFactory() {
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(usrName, password, brokerUrl);
		System.out.println("===========mq已建立连接" + brokerUrl + "===========");
		return activeMQConnectionFactory;
	}

	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setConnectionFactory(connectionFactory);
		return bean;
	}

	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		// 设置为发布订阅方式, 默认情况下使用的生产消费者方式
		bean.setPubSubDomain(true);
		bean.setConnectionFactory(connectionFactory);
		return bean;
	}
}
